package net.softwarefabrik.bde.core.model.platform;

import java.util.ArrayList;
import java.util.Collection;

import net.softwarefabrik.bde.core.model.BDEConstants;
import net.softwarefabrik.bde.core.model.BDEException;

/**
 * A class which provides information about a version of an OSGi platform as
 * well as some simple static methods to read or store data using strings.
 * 
 * @author Matthias Kuespert
 */
public class PlatformVersionInfo {

    /**
     * The name of the OSGi platform this version belongs to.
     */
    private String platformName = null;

    /**
     * The version identifier.
     */
    private String version      = null;

    /**
     * Parses the given string for <code>PlatformVersionInfo</code> objects.
     * 
     * @param data The string to parse.
     * @return A <code>Collection</code> of <code>PlatformVersionInfo</code>
     *         objects.
     * @throws BDEException if an error occurs during parsing.
     */
    public static Collection<PlatformVersionInfo> readData(String data) throws BDEException {
        Collection<PlatformVersionInfo> result = new ArrayList<PlatformVersionInfo>();
        //
        String[] entries = data.split(BDEConstants.STD_VALUE_SEPARATOR);
        for (String entry : entries) {
            result.add(new PlatformVersionInfo(entry));
        }
        return result;
    }

    /**
     * Stores the given data into a string.
     * 
     * @param data A <code>Collection</code> of
     *        <code>PlatformVersionInfo</code> objects to store.
     * @return A <code>String</code> containing a parsable representation of
     *         the data.
     */
    public static String writeDataToString(Collection<PlatformVersionInfo> data) {
        String result = BDEConstants.EMPTY_STRING;
        //
        for (PlatformVersionInfo entry : data) {
            if (!BDEConstants.EMPTY_STRING.equals(result)) {
                result += BDEConstants.STD_VALUE_SEPARATOR;
            }
            result += entry.toString();
        }
        return result;
    }

    /**
     * Initializing constructor to create a <code>PlatformVersionInfo</code>
     * from data stored in a <code>String</code>.
     * 
     * @param data The <code>String</code> which contains the data to parse.
     * @throws BDEException if something goes wrong
     */
    public PlatformVersionInfo(String data) throws BDEException {
        String[] parts = data.split(BDEConstants.STD_LIST_VALUE_SEPARATOR);
        if (parts.length < 2) {
            throw new BDEException("Could not parse PlatformVersionInfo data: " + data);
        }
        this.platformName = parts[0];
        this.version = parts[1];
    }

    /**
     * Initializing constructor to create a <code>PlatformVersionInfo</code>
     * from the given data.
     * 
     * @param platformName The name of the platform.
     * @param version The version identifier.
     */
    public PlatformVersionInfo(String platformName, String version) {
        this.platformName = platformName;
        this.version = version;
    }

    /**
     * @see Object.toString()
     * 
     * @return A <code>String</code> representation of this
     *         <code>PlatformVersionInfo</code> which can be parsed via the
     *         PlatformVersionInfo(String) constructor.
     */
    @Override
    public String toString() {
        return getPlatformName()
               + BDEConstants.STD_LIST_VALUE_SEPARATOR
               + getVersion();
    }

    /**
     * @return The name of the platform this <code>PlatformVersionInfo</code>
     *         object belongs to.
     */
    public final String getPlatformName() {
        return platformName;
    }

    /**
     * Sets the name of the platform this <code>PlatformVersionInfo</code>
     * object belongs to.
     * 
     * @param platformName The name of the platform.
     */
    public final void setPlatformName(String platformName) {
        this.platformName = platformName;
    }

    /**
     * @return The version identifier.
     */
    public final String getVersion() {
        return version;
    }

    /**
     * Sets the version identifier.
     * 
     * @param version A version identifier.
     */
    public final void setVersion(String version) {
        this.version = version;
    }
}
